package com.chunuo.blogs.service.impl;

import com.chunuo.blogs.dao.TagDao;
import com.chunuo.blogs.entity.Tag;
import com.chunuo.blogs.service.TagService;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.*;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 标签
 *
 * @author liujiamiao
 * @email 2544296058@qq.com
 * @date 2020-05-17 12:10
 */
@CacheConfig(cacheManager = "redisCacheManager",cacheNames={"tag"})
@Service
public class TagServiceImpl implements TagService {

    @Autowired
    private TagDao tagDao;

    @CacheEvict(key = "#page+'-'+#size",value = "tagAll")
    @Override
    public List<Tag> findAll(Integer page,Integer size){
        if(page!=0)
            PageHelper.startPage(page,size);
        return tagDao.findAll();
    }

    @Cacheable(key = "#id")
    @Override
    public Tag findById(Integer id) {
        return tagDao.findById(id);
    }

    @CacheEvict(value = "tagAll",allEntries = true)
    @CachePut(key = "#result.id")
    @Override
    public Tag add(Tag tag) {
        tagDao.add(tag);
        return tag;
    }

//    @CacheEvict(key = "#id")
    @Caching(evict = {@CacheEvict(key = "#id"),@CacheEvict(value = "tagAll",allEntries = true)})
    @Override
    public void delete(Integer id) {
        tagDao.delete(id);
    }


    @CacheEvict(value = "tagAll",allEntries = true)
    @CachePut(key = "#resutl.id")
    @Override
    public Tag update(Tag tag) {
        tagDao.update(tag);
        return tag;
    }
}
